#include <stdio.h>
#include <stdlib.h>
#include "camellia.h"
#include <time.h>
#include <inttypes.h>
int64_t timespecDiff(struct timespec *timeA_p, struct timespec *timeB_p)
{
	  return ((timeA_p->tv_sec * 1000000000) + timeA_p->tv_nsec) -
	             ((timeB_p->tv_sec * 1000000000) + timeB_p->tv_nsec);
}
int main(int argc, char* argv[])
{
	struct timespec start, end;
	CamImage source, down2xImage;
	camLoadPGM(&source, argv[1]);
	CamMeasuresResults results;
	
	time_t beg = time(NULL);
	int i;
	int operation =atoi(argv[2]);
	int returnValue=0;
	float averageDeviation=0.0f;
	const int iterations = 100;
	clock_gettime(CLOCK_MONOTONIC, &start);
	for( i=0; i < iterations; i++)
	{
		switch(operation)
		{
			case 0:
				returnValue=camSumOfPixels(&source);
				break;
			case 1:
				returnValue=camMeasures(&source, &results);
				break;
			case 2:
				returnValue=camMeasures(&source, &results);
				averageDeviation=camMeasureAverageDeviation(&source, results.average);
				break;
			default:
				returnValue=camSumOfPixels(&source);
				break;
		}
	}
	clock_gettime(CLOCK_MONOTONIC, &end);
	uint64_t timeElapsed = timespecDiff(&end, &start);
	printf("%llu seconds for %d iterations\n", (unsigned long long) timeElapsed, iterations);
	switch(operation)
	{
		case 0:
			printf("sumofallpixels: %d\n", returnValue);
			break;
		case 1:
			printf("sumofallpixels: %d\n", results.sum);
			printf("min: %d\n", results.ymin);
			printf("max: %d\n", results.ymax);
			printf("average: %d\n", results.average);
			break;
		case 2:
			printf("sumofallpixels: %d\n", results.sum);
			printf("min: %d\n", results.ymin);
			printf("max: %d\n", results.ymax);
			printf("average: %d\n", results.average);
			printf("averagedeviation: %f\n", averageDeviation);
			break;
		default:
			printf("sumofallpixels: %d\n", returnValue);
			break;
	}
	return 0;
}
